package com.tenement.controller.util;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadFileToLocalController {

	private static final Log logger = LogFactory.getLog(UploadFileToLocalController.class);

	// java.io.IOException: java.io.FileNotFoundException:
	// C:\Users\wk\AppData\Local\Temp\tomcat-docbase.1653534736980182812.80\tenement_images\14f04c70476faa2a261d983b5a11df48.jpg
	// (系统找不到指定的路径。)
	@RequestMapping(value = "/uploadFileToLocal.action")
	public String toCollectManageHtml(@RequestParam("aphoto") MultipartFile aphoto, HttpServletRequest request) {
		logger.info("【-上传图片到本地目录-】");
		String photo = aphoto.getOriginalFilename();
		try {

			aphoto.transferTo(
					new File(request.getSession().getServletContext().getRealPath("/tenement_images") + "\\" + photo));
			logger.info("【上传成功】");

		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "view/upload";
	}

	@RequestMapping(value = "/toUpLoadFileToLocalHtml.action")
	public String toCollectManageHtml() {
		return "view/upload";
	}

}
